package cn.itcast.service.impl;

import cn.itcast.dao.IAdminDao;
import cn.itcast.dao_impl.AdminDao;
import cn.itcast.entity.Admin;
import cn.itcast.exception.UserExistsException;
import cn.itcast.service.IAdminService;

/**
 * Created 吴海杰 on 2017/2/28.
 * 3.业务逻辑层实现
 */
public class AdminService implements IAdminService{
    private IAdminDao adminDao=new AdminDao();

    public Admin login(Admin admin) {
        try {
            return adminDao.findByNameAndPwd(admin);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public void register(Admin admin) throws UserExistsException
    {
        try {
            boolean flag=adminDao.userExists(admin.getuserName());
            if (flag){
                throw new UserExistsException("用户名已经存在，注册失败!");
            }
            adminDao.save(admin);
        } catch (UserExistsException e) {
            e.printStackTrace();
            throw e;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }


}
